package com.kkb.retailer.center.controller;


import com.kkb.retailer.center.api.MenuInfoService;
import com.kkb.retailer.center.api.RoleInfoService;
import com.kkb.retailer.center.api.UserInfoService;
import com.kkb.retailer.center.common.http.HttpResult;
import com.kkb.retailer.center.common.pojo.MenuInfo;
import com.kkb.retailer.center.common.pojo.MenuVo;
import com.kkb.retailer.center.common.pojo.UserInfo;
import com.kkb.retailer.center.common.util.SecurityUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/menus")
public class MenuInfoController {


    @Reference
    private UserInfoService userInfoService;

    @Reference
    private RoleInfoService roleInfoService;


    @Reference
    private MenuInfoService menuInfoService;

    //根据角色列表获取菜单列表
    @GetMapping(value = "/build")
    @ResponseBody
    public HttpResult buildMenus(){
        UserInfo user = userInfoService.findByUserName(SecurityUtils.getUserName());
        List<MenuInfo> menuDtoList = menuInfoService.findByRoles(roleInfoService.selectRoleByUserId(user.getId()));
        List<MenuInfo> menuInfos = (List<MenuInfo>)menuInfoService.buildTree(menuDtoList).get("content");
        List<MenuVo> menuVos = menuInfoService.buildMenus(menuInfos);

        return HttpResult.success(menuVos);

    }
 }
